﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TVProgramAnalyst.TimeLine;
using TPA.DataAccess;


namespace TVProgramAnalyst.Controls
{
    public partial class ProgramEditor : UserControl
    {
        public ProgramEditor()
        {
            InitializeComponent();
        }

        private void ProgramEditor_Load(object sender, EventArgs e)
        {
           
        }

        //List<ListItem> items;
        //List<ListItem> items_subcategory;
        //List<ListItem> items_ProducingArea;
        //List<ListItem> items_ProducingType;
        //List<ListItem> items_ProgramSource;
        private void LoadSystemCodeforDropDownList(ComboBox cbl, int systemcodeid)
        {
            //items = new List<ListItem>();
            LoadListItem(cbl, systemcodeid, 0);
            //cbl.DataSource=items;
            cbl.DisplayMember = "Text";
            cbl.ValueMember = "Value";
        }

        private void LoadListItem(ComboBox cbl, int systemcodeid, int level)
        {
            SystemCode code = BLLFactory.SystemCodeBLL.Get(systemcodeid);
            string indent = "";
            for (int i = 0; i <= level; i++)
                indent += "  ";
            level++;
            //if (level == 2)
            //{
            //    items.AddRange((from sc in code.SubSystemCodes
            //                    select new ListItem() { Text = indent + sc.Name, Value = sc.Id.ToString() }).ToList());
            //}
            //else
            //{
            foreach (SystemCode c in code.SubSystemCodes)
            {
                ListItem item = new ListItem() { Text = indent + c.Name, Value = c.Id.ToString() };
                //items.Add(item);
                cbl.Items.Add(item);
                if (c.SubSystemCodes.Count > 0)
                    LoadListItem(cbl, c.Id, level);
            }
            //}
            //cbl.DisplayMember = "Text";
            //cbl.ValueMember = "Value";
        }

        private Program _program;
        private SaveMode _savemode;

        //bind program details
        public  void SetProgram(Program p, SaveMode mode)
        {
            //load cbSubcategory
            cbSubcategory.Items.Clear();
            LoadSystemCodeforDropDownList(cbSubcategory, global.Default.Program_SubCategoryId);
            //load cbProducingArea
            cbProducingArea.Items.Clear();
            LoadSystemCodeforDropDownList(cbProducingArea, global.Default.Program_ProducingAreaId);
            //load cbProducingType
            cbProducingType.Items.Clear();
            LoadSystemCodeforDropDownList(cbProducingType, global.Default.Program_ProducingTypeId);
            //load cbProgramSource
            cbProgramSource.Items.Clear();
            LoadSystemCodeforDropDownList(cbProgramSource, global.Default.Program_ProgramSourceId);
            
            _savemode = mode;
            _program = p;
            if (_program == null)
                return;
            lblId.Text = _program.Id.ToString();
            txtDescription.Text = _program.Description;
            txtName.Text = _program.Name;
            txtEnglishDescription.Text = _program.EnglishDescription;
            txtEnglishName.Text = _program.EnglishName;
            txtTag.Text = "";
            foreach (ProgramProperty pp in _program.Properties)
            {
                txtTag.Text += pp.PropertyValue.Name + "; ";
            }
            if (_program.ProducingArea != null)
            {
                ListItem item = new ListItem() { Text = _program.ProducingArea.Name, Value = _program.ProducingAreaId.ToString() };
                cbProducingArea.SelectedItem = item;
            }
            else
            {
                cbProducingArea.SelectedIndex = -1;
            }
            if (p.ProducingType != null)
            {
                ListItem item = new ListItem() { Text = _program.ProducingType.Name, Value = _program.ProducingTypeId.ToString() };
                cbProducingType.SelectedItem = item;
            }
            else
            {
                cbProducingType.SelectedIndex = -1;
            }
            if (_program.ProgramSource != null)
            {
                ListItem item = new ListItem() { Text = _program.ProgramSource.Name, Value = _program.ProgramSourceId.ToString() };
                cbProgramSource.SelectedItem = item;
            }
            else
            {
                cbProgramSource.SelectedIndex = -1;
            }
            if (_program.Subcategory != null)
            {
                ListItem item = new ListItem() { Text = _program.Subcategory.Name, Value = _program.SubcategoryId.ToString() };
                cbSubcategory.SelectedItem = item;
            }
            else
            {
                cbSubcategory.SelectedIndex = -1;
            }
            //cbProducingArea.SelectedText = p.ProducingArea.Name;
            //cbProducingType.SelectedText = p.ProducingType.Name;
            //cbProgramSource.SelectedText = p.ProgramSource.Name;
            //cbSubcategory.SelectedText = p.Subcategory.Name;
            txtLengthInSeconds.Text = _program.LengthInSeconds.ToString();
            // dtProducingDate.Value = DateTime.Parse(p.ProducingDate);
        }

        public Program SaveProgram()
        {
            _program.Description = txtDescription.Text;
            _program.Name = txtName.Text;
            _program.EnglishDescription = txtEnglishDescription.Text;
            _program.EnglishName = txtEnglishName.Text;
            //_program.Tag=txtTag.Text  ;
            if (cbProducingArea.SelectedItem != null)
                _program.ProducingAreaId = int.Parse(((ListItem)cbProducingArea.SelectedItem).Value);
            else
                _program.ProducingAreaId = null;
            if (cbProducingType.SelectedItem != null)
                _program.ProducingTypeId = int.Parse(((ListItem)cbProducingType.SelectedItem).Value);
            else
                _program.ProducingTypeId = null;
            if (cbProgramSource.SelectedItem != null)
                _program.ProgramSourceId = int.Parse(((ListItem)cbProgramSource.SelectedItem).Value);
            else
                _program.ProgramSourceId = null;
            if (cbSubcategory.SelectedItem != null)
                _program.SubcategoryId = int.Parse(((ListItem)cbSubcategory.SelectedItem).Value);
            else
                _program.SubcategoryId = null;

            _program.LengthInSeconds = string.IsNullOrEmpty(txtLengthInSeconds.Text) ? 0 : int.Parse(txtLengthInSeconds.Text);
            _program.ProducingDate = dtProducingDate.Value.Date.ToShortDateString();
            if (_savemode == SaveMode.New)
            {
                _program.Id = Guid.NewGuid();
                BLLFactory.ProgramBLL.Add(_program);
                _savemode = SaveMode.Update;
            }
            BLLFactory.ProgramBLL.SaveChanges();
            return _program;
        }

        private void txtTag_Click(object sender, EventArgs e)
        {

            if (_program == null)
            {
                MessageBox.Show("Please select a program first!");
                return;
            }
            //SaveProgram();
            frmTagSelector frm = new frmTagSelector();
            frm.Program = this.SaveProgram();
            frm.ShowDialog(this);
            frm.Dispose();
            //refresh tag text
            txtTag.Text = "";
            foreach (ProgramProperty pp in _program.Properties)
            {
                txtTag.Text += pp.PropertyValue.Name + "; ";
            }
        }
    }
}
